<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 组件-确认乘客信息 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="component-modal-confirm-passenger" @click.stop>
		<uni-popup ref="popupModal" type="center" @change="onChange">
			<view class="popup-box" :style="{'--theme-color': themeColor}">
				<view class="popup-header">
					<view class="title">确认乘客信息</view>
					<view class="subtitle">请输入乘客手机尾号4位数</view>
					<image class="close" src="/static/close.png" mode="aspectFit" @click="onClose()"></image>
				</view>
				<view class="popup-content flex justify-content-between" v-if="inputValue">
					<input class="content-input" :class="{active: selectInput == index}" v-for="(item, index) in inputValue" :key="index" v-model="inputValue[index]" :focus="selectInput == index" type="number" maxlength="1" @input="changeValue(index)" @focus="selectInput = index" @blur="handleBlur(index)" />
				</view>
				<view class="popup-btn" @click="onConfirm">确认已上车</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		name: "componentModalConfirmPassenger",
		data() {
			return {
				// 手机号后四位
				inputValue: ["", "", "", ""],
				// 当前选中输入框
				selectInput: -1,
			};
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		methods: {
			// 打开模态框
			open() {
				this.inputValue = ["", "", "", ""]
				this.selectInput = -1
				this.$refs.popupModal.open()
			},
			// 关闭弹窗
			onClose() {
				this.$refs.popupModal.close()
			},
			// 改变事件
			onChange(e) {
				this.$emit("onChange", e.show)
			},
			// 改变输入框数值
			changeValue(index) {
				if (!this.inputValue[index] && index < 3) {
					this.selectInput = index
				} else if (index < 3 && !this.inputValue[index + 1]) {
					this.selectInput = index + 1
				}
			},
			// 输入框失去焦点事件
			handleBlur(index) {
				if (this.selectInput == index) {
					this.selectInput = -1
				}
			},
			// 确认
			onConfirm() {
				if (this.inputValue.some(v => !v)) {
					uni.showToast({
						icon: "none",
						title: "请输入乘客手机尾号4位数"
					})
					return
				}
				const number = this.inputValue.join("")
				this.$emit("onConfirm", number)
			},
		},
	}
</script>

<style lang="scss">
	.component-modal-confirm-passenger {
		position: relative;
		z-index: 999;

		.popup-box {
			background: #FFFFFF;
			border-radius: 32rpx;
			padding: 32rpx 32rpx 48rpx;
			width: calc(100vw - 64rpx);

			.popup-header {
				padding: 0 32rpx;
				position: relative;

				.title {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 48rpx;
				}

				.subtitle {
					color: #8D929C;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
					margin-top: 16rpx;
				}

				.close {
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					top: 0;
					right: 32rpx;
				}
			}

			.popup-content {
				padding: 48rpx 0;

				.content-input {
					width: 144rpx;
					height: 144rpx;
					border-radius: 32rpx;
					border: 1px solid #EBEBEB;
					background: #F7F7F7;
					box-sizing: border-box;
					text-align: center;
					color: #242629;
					font-size: 36rpx;
					font-weight: 600;

					&.active {
						border-color: var(--theme-color);
					}
				}
			}

			.popup-btn {
				color: #FFF;
				text-align: center;
				font-size: 28rpx;
				font-weight: 600;
				line-height: 40rpx;
				padding: 36rpx 48rpx;
				border-radius: 32rpx;
				background: var(--theme-color);
			}
		}
	}
</style>